package com.caifu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.ZcNoArchivesUser;
import com.caifu.service.ZcNoArchivesUserService;
import com.caifu.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 * 非档案人员表 前端控制器
 * </p>
 *
 * @author szt
 * @since 2020-11-09
 */
@Controller
@RequestMapping("/noArchivesUser")
public class ZcNoArchivesUserController extends AbstractController{

    @Autowired
    private ZcNoArchivesUserService noArchivesUserService;

    @PostMapping("save")
    @ResponseBody
    public Result save(String data){
        try{
            List<Map<String,String>> result = new ArrayList<>();
            if(StringUtils.isNotBlank(data)){
                Date now = new Date();
                int createBy = getUserNo();
                JSONArray arr = JSONArray.fromObject(data);
                if(arr != null && arr.size() > 0){
                    List<ZcNoArchivesUser> list = new ArrayList<>();
                    for(int i = 0 ; i < arr.size() ; i++){
                        JSONObject obj = arr.getJSONObject(i);
                        if(obj != null && obj.size() > 0){
                            ZcNoArchivesUser user = new ZcNoArchivesUser();
                            String id = "FD_"+Util.getStringRandom(12);
                            while (noArchivesUserService.count(new QueryWrapper<ZcNoArchivesUser>().eq("user_no",id)) > 0){
                                id = "FD_"+Util.getStringRandom(12);
                            }
                            user.setUserNo(id);
                            user.setUserName(obj.getString("name"));
                            user.setSex(obj.getString("sex"));
                            user.setType(obj.getString("type"));
                            user.setBrithDate(Util.stringToDate(obj.getString("birth_date"),"yyyy-MM-dd"));
                            user.setCreateTime(now);
                            user.setCreateBy(createBy+"");

                            Map<String,String> map = new HashMap<>();
                            map.put("user_name",user.getUserName());
                            map.put("user_no",user.getUserNo());
                            map.put("user_sex",user.getSex());
                            map.put("user_type",user.getType());
                            map.put("age",obj.getString("age"));

                            result.add(map);
                            list.add(user);
                        }
                    }
                    noArchivesUserService.saveBatch(list);
                }
            }
            return new Result(200,"保存用户成功！",result);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"保存用户失败！");
        }
    }
}
